<?php

namespace App\Listeners;

use App\Events\Consumption;
use App\Events\Distribution;
use App\Events\OrderIntegral;
use App\Events\OrderPaid;
use App\Events\VirtualGoods;
use App\OrderAction;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class HandleOrderPaid
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  OrderPaid  $event
     * @return void
     */
    public function handle(OrderPaid $event)
    {
        $order = $event->order;
        // 特殊商品订单处理
        if ($order->pay_status == 1) {
            OrderAction::newAction($order, ['action' => '支付成功', 'note' => '订单支付成功', 'user_id' => $order->user_id, 'details' => '']);
            // 处理分销
            event(new Distribution($order));
            // 积分返还
            event(new OrderIntegral($order));
            // 处理拼团
            if ($order->promotion_type == 'App\TeamActivity') {
                event(new HandleTeamActivityPaid($order));
            }
            // 处理消费（消费统计+等级）
            event(new Consumption($order));

            //处理虚拟商品
            event(new VirtualGoods($order));
        }
    }
}
